#include "LevelSingleDock.h"

#include "GameplayBackground.h"

LevelSingleDock::~LevelSingleDock()
{
}

void LevelSingleDock::Load( Dock* docks, GameplayBackground* background ) 
{
	background->Load( "level1_shore_bg_top", "level1_shore_bg_bottom" );   
	
	// setup the docks
	docks[0].Load( CIwFVec2(229, 39), CIwFVec2(1,0), DockType::RED );
	
	// setup the second dock to be outside the screen
	docks[1].Load( CIwFVec2(-800, -800), CIwFVec2(-1,0), DockType::YELLOW );
	
	m_state = LevelSingleDockState::START;
	m_active_ships = 0;
}

void LevelSingleDock::Unload()
{ }

LevelUpdateReturnStatus::Enum LevelSingleDock::Update( DockType::Enum *type, CIwFVec2* p1, CIwFVec2* p2, float dt )
{
	if( m_state == LevelSingleDockState::START ) {
		// start of the first ship
		p1->x = 520;
		p1->y = 250;
		
		p2->x = 480;
		p2->y = 250;
		
		*type = DockType::RED;
		
		m_state = LevelSingleDockState::WAITING_FOR_FIRST_DOCKING;
		
		return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_state == LevelSingleDockState::WAITING_FOR_FIRST_DOCKING ) {
	
		if( m_ship->IsDocking() == true ) {
		
			// start one ship from the right
			p1->x = 540;
			p1->y = 50;
			
			p2->x = 520;
			p2->y = 50;
			
			*type = DockType::RED;
			
			m_state = LevelSingleDockState::WAIT_FOR_START_OF_THIRD_SHIP;
			
			return LevelUpdateReturnStatus::START_SHIP;
		}
	}
	
	if( m_state == LevelSingleDockState::WAIT_FOR_START_OF_THIRD_SHIP ) {
		// start one ship from the right
		p1->x = -120;
		p1->y = 50;
		
		p2->x = -100;
		p2->y = 50;
		
		*type = DockType::RED;
		
		m_state = LevelSingleDockState::WAITING_FOR_FIRST_SHIP_EXIT;
		
		return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_state == LevelSingleDockState::WAITING_FOR_FIRST_SHIP_EXIT ) {
		// now start event more ships
		
		if( m_ship->OutsideScreen() == true ) {
			m_state = LevelSingleDockState::START_SECOND_WAVE;
			
			// start the first ship of the wave	
			p1->x = -20;
			p1->y = 10;
			
			p2->x = 0;
			p2->y = 50;
			
			*type = DockType::RED;
			
			return LevelUpdateReturnStatus::START_SHIP;
		}
	}
	
	if( m_state == LevelSingleDockState::START_SECOND_WAVE ) {
	
			// start the first ship of the wave	
			p1->x = 530;
			p1->y = 120;
			
			p2->x = 480;
			p2->y = 60;
			
			*type = DockType::RED;
			
			m_state = LevelSingleDockState::WAIT_THIRD_WAVE;
			m_time_for_next_ship = 0;
			
			return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_state == LevelSingleDockState::WAIT_THIRD_WAVE ) {
		m_time_for_next_ship += dt;
		
		if( m_time_for_next_ship >= 5.0f ) {
			m_state = LevelSingleDockState::START_THIRD_WAVE;
			m_time_for_next_ship = 0;
		}
	}
	
	if( m_state == LevelSingleDockState::START_THIRD_WAVE ) {
		p1->x = 530;
		p1->y = 320;
		
		p2->x = 480;
		p2->y = 220;
		
		*type = DockType::RED;
		
		m_state = LevelSingleDockState::START_THIRD_WAVE_TWO;
		
		return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_state == LevelSingleDockState::START_THIRD_WAVE_TWO ) {
		m_time_for_next_ship += dt;
	
		if( m_time_for_next_ship >= 0.5f )  {
			p1->x = -20;
			p1->y = 160;
			
			p2->x = 0;
			p2->y = 160;
			
			*type = DockType::RED;
			
			m_state = LevelSingleDockState::WAIT_FOR_END;
			
			return LevelUpdateReturnStatus::START_SHIP;
		}
	}
	
	if( m_state == LevelSingleDockState::WAIT_FOR_END ) {
		// now wait until all ships left the screen
		if( m_active_ships == 0 ) {
			return LevelUpdateReturnStatus::LEVEL_COMPLETE;
		}
	}

	return LevelUpdateReturnStatus::DO_NOTHING; 
}

void LevelSingleDock::ShipAdded( Ship *ship ) 
{
	++ m_active_ships;
	if( m_state == LevelSingleDockState::WAITING_FOR_FIRST_DOCKING ) {
		// store the first ship, we need data to start the next
		// wave of ships
		m_ship = ship;
	}
}

void LevelSingleDock::ShipOutOfScreen( Ship *ship )
{
	-- m_active_ships;
}

void LevelSingleDock::Draw ()
{ }